package com.mikey.lock8;

import java.util.concurrent.TimeUnit;

/**
 * @author :Mikey
 * @description :
 * 5，增加静态的同步方法，发短信在前，
 * 6，两个对象，同上，锁class模板
 * @create :2022-09-20 15:26:00
 */
public class Test2 {
    public static void main(String[] args) {
        //两个对象的class类模板只有一个，static，锁的是class
        Phone2 phone1 = new Phone2();
        Phone2 phone2 = new Phone2();
        new Thread(() -> {
            phone1.sendMsg();
        }, "A").start();
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new Thread(() -> {
            phone2.call();
        }, "B").start();
    }
}

class Phone2 {
    //synchronized锁对象调用者
    //static 静态方法，类一加载就有！锁的是class
    public static synchronized void sendMsg() {
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("发短信");
    }

    public static synchronized void call() {
        System.out.println("打电话");
    }
}
